import API from '@/api';

const state = {
  communityType: [], // 社群类型
  villageList: [], // 小区列表
}

const getters = {
  communityObj: ({ communityType }) => {
    return communityType.reduce(
      (acc, cur) => ((acc[cur.value] = cur.label), acc),
      {}
    );
  }
}

const mutations = {
  SET_COMMUNITY: (state, list) => {
    state.communityType = list
  },
  SET_VILLAGE: (state, list) => {
    state.villageList = list
  },
}

const actions = {
  async initCommunity({ commit }) {
    let { data } = await API.getAsociationTypeList({ page_size: 100 })
    commit('SET_COMMUNITY', data.map(v => ({ value: v.id, label: v.name })))
  },
  async initVillage({ commit }) {
    let { data } = await API.getCommunityList({ page_size: 100 })
    commit('SET_VILLAGE', data.map(v => ({ value: v.id, label: v.name })))
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

